/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package config.items;

/**
 * Defines the general configuration items stored under "/".
 * @author Christian Masus
 *
 */
public class GeneralItems {

	// Allowed values of specific types
	private final String[] GUI_TYPES = {"JavaGui", "JavaGuiNative", "Debug", "Text", "WebGui"};

	// Default values
	private final String  DEFAULT_GUI_TYPE           = "JavaGui";
	private final boolean DEFAULT_GUI_HIDDEN         = false;
	private final int     DEFAULT_GUI_POS_X          = 0;
	private final int     DEFAULT_GUI_POS_Y          = 0;
	private final int     DEFAULT_GUI_WIDTH          = 800;
	private final int     DEFAULT_GUI_HEIGHT         = 400;
	private final boolean DEFAULT_LISTEN_ENABLED     = false;
	private final int     DEFAULT_LISTEN_PORT        = 45678;
	private final boolean DEFAULT_AUTO_SYNC          = false;
	private final int     DEFAULT_AUTO_SYNC_INTERVAL = 5;

	// Configuration
	private String guiType;
	private boolean guiHidden;
	private int guiPosX;
	private int guiPosY;
	private int guiWidth;
	private int guiHeight;
	private boolean listenEnabled;
	private int listenPort;
	private boolean autoSync;
	private int autoSyncInterval; // Value in minutes


	public GeneralItems() {
		loadDefaultValues();
	}


	public String getGuiType() {
		return guiType;
	}
	public void setGuiType(String guiType) {
		boolean set = false;
		for(int i = 0; i < GUI_TYPES.length; i++) {
			if(guiType.equals(GUI_TYPES[i])) {
				this.guiType = guiType;
				set = true;
			}
		}
		if(set == false) { // Set to default
			this.guiType = DEFAULT_GUI_TYPE;
		}
	}

	public boolean getGuiHidden() {
		return guiHidden;
	}
	public void setGuiHidden(boolean guiHidden) {
		this.guiHidden = guiHidden;
	}
	public void setGuiHidden(String guiHidden) {
		this.guiHidden = new Boolean(guiHidden);
	}

	public int getGuiPosX() {
		return guiPosX;
	}
	public void setGuiPosX(int guiPosX) {
		if(guiPosX < DEFAULT_GUI_POS_X) {
			this.guiPosX = DEFAULT_GUI_POS_X;
		}
		else {
			this.guiPosX = guiPosX;
		}
	}
	public void setGuiPosX(String guiPosX) {
		setGuiPosX(new Integer(guiPosX));
	}

	public int getGuiPosY() {
		return guiPosY;
	}
	public void setGuiPosY(int guiPosY) {
		if(guiPosY < DEFAULT_GUI_POS_Y) {
			this.guiPosY = DEFAULT_GUI_POS_Y;
		}
		else {
			this.guiPosY = guiPosY;
		}
	}
	public void setGuiPosY(String guiPosY) {
		setGuiPosY(new Integer(guiPosY));
	}

	public int getGuiWidth() {
		return guiWidth;
	}
	public void setGuiWidth(int guiWidth) {
		if(guiWidth < 50) {
			guiWidth = 50;
		}
		else {
			this.guiWidth = guiWidth;
		}
	}
	public void setGuiWidth(String guiWidth) {
		setGuiWidth(new Integer(guiWidth));
	}

	public int getGuiHeight() {
		return guiHeight;
	}
	public void setGuiHeight(int guiHeight) {
		if(guiHeight < 50) {
			this.guiHeight = 50;
		}
		else {
			this.guiHeight = guiHeight;
		}
	}
	public void setGuiHeight(String guiHeight) {
		setGuiHeight(new Integer(guiHeight));
	}

	public boolean getListenEnabled() {
		return listenEnabled;
	}
	public void setListenEnabled(boolean listenEnabled) {
		this.listenEnabled = listenEnabled;
	}
	public void setListenEnabled(String listenEnabled) {
		this.listenEnabled = new Boolean(listenEnabled);
	}

	public int getListenPort() {
		return listenPort;
	}
	public void setListenPort(int listenPort) {
		if(isInternetPort(listenPort)) {
			this.listenPort = listenPort;
		}
		else {
			this.listenPort = DEFAULT_LISTEN_PORT;
		}
	}
	public void setListenPort(String listenPort) {
		setListenPort(new Integer(listenPort));
	}

	public boolean getAutoSync() {
		return autoSync;
	}
	public void setAutoSync(boolean autoSync) {
		this.autoSync = autoSync;
	}
	public void setAutoSync(String autoSync) {
		this.autoSync = new Boolean(autoSync);
	}

	public int getAutoSyncInterval() {
		return autoSyncInterval;
	}
	public void setAutoSyncInterval(int autoSyncInterval) {
		if(autoSyncInterval < 1) {
			this.autoSyncInterval = 1;
		}
		else if(autoSyncInterval > Integer.MAX_VALUE) {
			this.autoSyncInterval = Integer.MAX_VALUE;
		}
		else {
			this.autoSyncInterval = autoSyncInterval;
		}
	}
	public void setAutoSyncInterval(String autoSyncInterval) {
		setAutoSyncInterval(new Integer(autoSyncInterval));
	}



	private boolean isInternetPort(int port) {
		if(port >= 0 && port <= 65535) {
			return true;
		}
		return false;
	}



	public void loadDefaultValues() {
		guiType          = DEFAULT_GUI_TYPE;
		guiHidden        = DEFAULT_GUI_HIDDEN;
		guiPosX          = DEFAULT_GUI_POS_X;
		guiPosY          = DEFAULT_GUI_POS_Y;
		guiWidth         = DEFAULT_GUI_WIDTH;
		guiHeight        = DEFAULT_GUI_HEIGHT;
		listenEnabled    = DEFAULT_LISTEN_ENABLED;
		listenPort       = DEFAULT_LISTEN_PORT;
		autoSync         = DEFAULT_AUTO_SYNC;
		autoSyncInterval = DEFAULT_AUTO_SYNC_INTERVAL;
	}

	public String[][] getTagValuePair() {
		String[][] ret = {
				{"GuiType",          guiType},
				{"GuiHidden",        new Boolean(guiHidden).toString()},
				{"GuiPosX",          new Integer(guiPosX).toString()},
				{"GuiPosY",          new Integer(guiPosY).toString()},
				{"GuiWidth",         new Integer(guiWidth).toString()},
				{"GuiHeight",        new Integer(guiHeight).toString()},
				{"ListenEnabled",    new Boolean(listenEnabled).toString()},
				{"ListenPort",       new Integer(listenPort).toString()},
				{"AutoSync",         new Boolean(autoSync).toString()},
				{"AutoSyncInterval", new Integer(autoSyncInterval).toString()}
		};
		return ret;
	}

	public String toString() {
		String ret = "";
		String[][] tagValuePair = getTagValuePair();
		for(int row = 0; row < tagValuePair.length; row++) {
			ret += tagValuePair[row][0] + " = " + tagValuePair[row][1] + "\n";
		}
		return ret;
	}
}
